import { expect } from '@jest/globals';
import path from 'path';

import {
  buildAppAndCheckFile,
  buildHapsAndCheckFile,
  cleanTarget,
  listZipEntries,
  makeOneHap
} from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { Project } from '../../plugins/project-builder/models/project-models';
import { ICaseRecord } from '../../plugins/report-builder/report-models';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption, TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const testFun: TestCaseFun = async (project: Project, taskConfig: TestTaskOption, record: ICaseRecord) => {
  // 1. 选择product-default-entry-default进行make构建
  await makeOneHap('entry')(project, taskConfig, record);
  await cleanTarget(project);

  // 2. 选择product-default-feature-default进行make构建
  await makeOneHap('application')(project, taskConfig, record);
  await cleanTarget(project);

  // 3. 选择product-default-entry-default和feature-default进行菜单栏build-hap构建
  // build-hap构建成功，在entry-build-default目录下生成一个entry-default-hap包，在feature-build-default目录下生成一个feature-default-hap包
  await buildHapsAndCheckFile(project, taskConfig, record);
  await cleanTarget(project);

  // 4. 选择product-default-entry-default和feature-default进行菜单栏build-app构建
  // build-app构建成功，在工程-build-default目录下生成一个myapplication-default-app包，且app包含有entry-build-default包和feature-default-hap包
  await buildAppAndCheckFile(project, taskConfig, record);
  // eslint-disable-next-line max-len
  const appPath = path.resolve(project.projectRootPath(), 'build', 'outputs', 'default', 'myapplication-default-unsigned.app');
  const files = (await listZipEntries(appPath)).map(item => item[0]).filter(item => item.includes('.hap'));

  // eslint-disable-next-line max-len
  const existHaps = files.length === 2 && files.some(item => item.includes('entry')) && files.some(item => item.includes('application'));
  expect(existHaps).toBe(true);
};

const projectBuilderOptionFa: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.FA,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: [ModuleTemplateType.EmptyAbility]
};

const projectBuilderOptionStage: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.FA,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: [ModuleTemplateType.EmptyAbility]
};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_Build_Visualization_Target_FA_API10_ArkTs_Multi_Module_001')
  .description('1.API10-FA多模块工程entry-feature,工程只有1个product，两个模块均只有1个product，1个target，且target配置一致')
  .projectBuilderOption(projectBuilderOptionFa)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L1).build())
  .testFun(testFun)
  .load();

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_Build_Visualization_Target_HOS_api10_Stage_Multi_Module_001')
  .description('1.api10-Stage多模块工程entry-feature,工程只有1个product，两个模块均只有1个product，1个target，且target配置一致')
  .projectBuilderOption(projectBuilderOptionStage)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L1).build())
  .testFun(testFun)
  .load();
